package com.ourteam.product.requirement.dao.impl;

import com.ourteam.product.requirement.dao.IProductGlossaryDAO;
import com.ourteam.product.requirement.dao.ProductGlossary;
import com.ourteam.product.requirement.dao.ProductGlossaryDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 CST 2017
 * Auto Generate Date : 2018-05-03 16:45:39
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractProductGlossaryDAOJdbcImpl extends AbstractSpringDAO
    implements IProductGlossaryDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, Code, Content, CreateDate, CreatorId, LastEditorId, LastEditDate,
            Name, ProductId, Remarks, Status, SinceVersionId, Type,
            LastEditorName, SinceVersion, ProductDefineId
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.Code,
            _DataObjectProperty.Content, _DataObjectProperty.CreateDate,
            _DataObjectProperty.CreatorId, _DataObjectProperty.LastEditorId,
            _DataObjectProperty.LastEditDate, _DataObjectProperty.Name,
            _DataObjectProperty.ProductId, _DataObjectProperty.Remarks,
            _DataObjectProperty.Status, _DataObjectProperty.SinceVersionId,
            _DataObjectProperty.Type, _DataObjectProperty.LastEditorName,
            _DataObjectProperty.SinceVersion,
            _DataObjectProperty.ProductDefineId
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<ProductGlossary, ProductGlossaryDAOQueryBean> daoHelper;

    public AbstractProductGlossaryDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<ProductGlossary, ProductGlossaryDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysUserDAO.EntityName)
                                .append(" lastEditorA ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysUserDAO.Id,
                    com.ourteam.system.dao.ISysUserDAO.EntityName + ".",
                    "lastEditorA."));
            relatedJoinSQLBuffer.append(" = ").append(LastEditorId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.product.requirement.dao.IProductVersionDAO.EntityName)
                                .append(" productVersionA ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.product.requirement.dao.IProductVersionDAO.Id,
                    com.ourteam.product.requirement.dao.IProductVersionDAO.EntityName +
                    ".", "productVersionA."));
            relatedJoinSQLBuffer.append(" = ").append(SinceVersionId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aProductGlossary
         * @throws Exception
         */
    public boolean checkIsUnique(ProductGlossary aProductGlossary)
        throws Exception {
        if (aProductGlossary == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aProductGlossary.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aProductGlossary
     * @throws Exception
     */
    public void insert(ProductGlossary aProductGlossary)
        throws Exception {
        PropertyUtils.setProperty(aProductGlossary,
            _DataObjectProperty.LastEditDate,
            new java.sql.Timestamp(System.currentTimeMillis()));

        PropertyUtils.setProperty(aProductGlossary,
            _DataObjectProperty.CreateDate,
            new java.sql.Timestamp(System.currentTimeMillis()));

        super.doInsert(aProductGlossary);
    }

    /**
         * Batch Insert Datas
         * @param aProductGlossary
         * @throws Exception
         */
    public void batchInsert(ProductGlossary[] aProductGlossarys)
        throws Exception {
        super.doBatchInsert(aProductGlossarys);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(ProductGlossaryDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(ProductGlossary aProductGlossary)
        throws Exception {
        delete(aProductGlossary.getId());
    }

    /**
     * Update Data Selective
     * @param aProductGlossary
     * @throws Exception
     */
    public void update(ProductGlossary aProductGlossary)
        throws Exception {
        PropertyUtils.setProperty(aProductGlossary,
            _DataObjectProperty.LastEditDate,
            new java.sql.Timestamp(System.currentTimeMillis()));

        if (aProductGlossary.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aProductGlossary);

        ProductGlossary tempObj = findById(aProductGlossary.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aProductGlossary, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aProductGlossarys[]
     * @throws Exception
     */
    public void batchUpdate(ProductGlossary[] aProductGlossarys)
        throws Exception {
        super.doBatchUpdate(aProductGlossarys);
    }

    /**
         * Update Data By Condictions
         * @param aProductGlossary
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(ProductGlossary aProductGlossary,
        ProductGlossaryDAOQueryBean aQueryBean) throws Exception {
        PropertyUtils.setProperty(aProductGlossary,
            _DataObjectProperty.LastEditDate,
            new java.sql.Timestamp(System.currentTimeMillis()));

        super.doUpdateByQuery(aProductGlossary, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(ProductGlossaryDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(ProductGlossary aProductGlossary)
        throws Exception {
        if (aProductGlossary.getCode() == null) {
            throw new Exception(
                "The ProductGlossary.code value is not allow NULL");
        }

        if (aProductGlossary.getContent() == null) {
            throw new Exception(
                "The ProductGlossary.content value is not allow NULL");
        }

        if (aProductGlossary.getCreateDate() == null) {
            throw new Exception(
                "The ProductGlossary.createDate value is not allow NULL");
        }

        if (aProductGlossary.getLastEditDate() == null) {
            throw new Exception(
                "The ProductGlossary.lastEditDate value is not allow NULL");
        }

        if (aProductGlossary.getName() == null) {
            throw new Exception(
                "The ProductGlossary.name value is not allow NULL");
        }

        if (aProductGlossary.getRemarks() == null) {
            throw new Exception(
                "The ProductGlossary.remarks value is not allow NULL");
        }

        if (aProductGlossary.getStatus() == null) {
            throw new Exception(
                "The ProductGlossary.status value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public ProductGlossary findById(final long id) throws Exception {
        ProductGlossaryDAOQueryBean queryBean = new ProductGlossaryDAOQueryBean();

        ProductGlossaryDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        ProductGlossary[] ProductGlossarys = query(queryBean);

        if (ProductGlossarys.length == 0) {
            return null;
        } else {
            return ProductGlossarys[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return ProductGlossary[]
         * @throws Exception
         */
    public ProductGlossary[] query(ProductGlossaryDAOQueryBean queryBean)
        throws Exception {
        List result = super.doQuery(queryBean,
                new JdbcProductGlossaryRowMapper());

        return (ProductGlossary[]) result.toArray(new ProductGlossary[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        ProductGlossaryDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(ProductGlossaryDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(ProductGlossary[] aProductGlossarys,
        ProductGlossaryDAOQueryBean queryBean) throws Exception {
        daoHelper.synchronize(aProductGlossarys, queryBean);
    }

    public void synchronize(ProductGlossary[] aProductGlossarys,
        ProductGlossaryDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<ProductGlossary> callBack)
        throws Exception {
        daoHelper.synchronize(aProductGlossarys, queryBean, callBack);
    }

    public static class JdbcProductGlossaryRowMapper implements RowMapper<ProductGlossary> {
        public ProductGlossary mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            ProductGlossary object = new ProductGlossary();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object code = null;

                code = resultSet.getObject(columnIndex++);

                object.setCodeNull(resultSet.getBoolean(columnIndex++));

                if (code != null) {
                    setBeanValue(object, _DataObjectProperty.Code, code);
                }

                Object content = null;

                content = resultSet.getObject(columnIndex++);

                object.setContentNull(resultSet.getBoolean(columnIndex++));

                if (content != null) {
                    setBeanValue(object, _DataObjectProperty.Content, content);
                }

                Object createDate = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    createDate = resultSet.getTimestamp(columnIndex++);
                } else {
                    createDate = resultSet.getObject(columnIndex++);
                }

                object.setCreateDateNull(resultSet.getBoolean(columnIndex++));

                if (createDate != null) {
                    setBeanValue(object, _DataObjectProperty.CreateDate,
                        createDate);
                }

                Object creatorId = null;

                creatorId = resultSet.getObject(columnIndex++);

                object.setCreatorIdNull(resultSet.getBoolean(columnIndex++));

                if (creatorId != null) {
                    setBeanValue(object, _DataObjectProperty.CreatorId,
                        creatorId);
                }

                Object lastEditorId = null;

                lastEditorId = resultSet.getObject(columnIndex++);

                object.setLastEditorIdNull(resultSet.getBoolean(columnIndex++));

                if (lastEditorId != null) {
                    setBeanValue(object, _DataObjectProperty.LastEditorId,
                        lastEditorId);
                }

                Object lastEditDate = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    lastEditDate = resultSet.getTimestamp(columnIndex++);
                } else {
                    lastEditDate = resultSet.getObject(columnIndex++);
                }

                object.setLastEditDateNull(resultSet.getBoolean(columnIndex++));

                if (lastEditDate != null) {
                    setBeanValue(object, _DataObjectProperty.LastEditDate,
                        lastEditDate);
                }

                Object name = null;

                name = resultSet.getObject(columnIndex++);

                object.setNameNull(resultSet.getBoolean(columnIndex++));

                if (name != null) {
                    setBeanValue(object, _DataObjectProperty.Name, name);
                }

                Object productId = null;

                productId = resultSet.getObject(columnIndex++);

                object.setProductIdNull(resultSet.getBoolean(columnIndex++));

                if (productId != null) {
                    setBeanValue(object, _DataObjectProperty.ProductId,
                        productId);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object sinceVersionId = null;

                sinceVersionId = resultSet.getObject(columnIndex++);

                object.setSinceVersionIdNull(resultSet.getBoolean(columnIndex++));

                if (sinceVersionId != null) {
                    setBeanValue(object, _DataObjectProperty.SinceVersionId,
                        sinceVersionId);
                }

                Object type = null;

                type = resultSet.getObject(columnIndex++);

                object.setTypeNull(resultSet.getBoolean(columnIndex++));

                if (type != null) {
                    setBeanValue(object, _DataObjectProperty.Type, type);
                }

                Object lastEditorName = null;

                lastEditorName = resultSet.getObject(columnIndex++);

                object.setLastEditorNameNull(resultSet.getBoolean(columnIndex++));

                if (lastEditorName != null) {
                    setBeanValue(object, _DataObjectProperty.LastEditorName,
                        lastEditorName);
                }

                Object sinceVersion = null;

                sinceVersion = resultSet.getObject(columnIndex++);

                object.setSinceVersionNull(resultSet.getBoolean(columnIndex++));

                if (sinceVersion != null) {
                    setBeanValue(object, _DataObjectProperty.SinceVersion,
                        sinceVersion);
                }

                Object productDefineId = null;

                productDefineId = resultSet.getObject(columnIndex++);

                object.setProductDefineIdNull(resultSet.getBoolean(
                        columnIndex++));

                if (productDefineId != null) {
                    setBeanValue(object, _DataObjectProperty.ProductDefineId,
                        productDefineId);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
